package net.wanho.service.sys.impl;


import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import net.wanho.async.AsyncFactory;
import net.wanho.async.AsyncTask;
import net.wanho.mapper.sys.UserMapper;
import net.wanho.po.sys.User;
import net.wanho.service.sys.UserServiceI;
import net.wanho.util.ServletUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @项目名称: [cms]
 * @类名称: [UserServiceImpl]
 * @类描述: [用户信息表业务逻辑层实现]
 * @创建人: [choco]
 * @创建时间: [2020-07-30 09:36:32]
 * @修改人: []
 * @修改时间: []
 * @修改备注: []
 * @版本: []
 * @版权: []
 */
@Service
@Slf4j
public class UserServiceImpl implements UserServiceI {

    @Autowired
    private UserMapper userMapper;

    /**
     * @方法名: [selectUserById]
     * @方法描述: [查询用户信息表信息]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 09:36:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public User selectUserById(Long userId) {
        try {
            return userMapper.selectUserById(userId);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * @方法名: [selectUserList]
     * @方法描述: [查询用户信息表列表]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 09:36:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public List<User> selectUserList(User user) {
        try {
            return userMapper.selectUserList(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * @方法名: [insertUser]
     * @方法描述: [新增用户信息表信息]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 09:36:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public int insertUser(@RequestBody User user) {
        try {
            return userMapper.insertUser(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * @方法名: [updateUser]
     * @方法描述: [修改用户信息表信息]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 09:36:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public int updateUser(@RequestBody User user) {
        try {
            return userMapper.updateUser(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * @方法名: [deleteUserByIds]
     * @方法描述: [删除用户信息表信息]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 09:36:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public int deleteUserByIds(String ids) {
        try {
            return userMapper.deleteUserByIds(ids.split(","));
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * @方法名: [findUserByName]
     * @方法描述: [根据用户名查询信息表信息]
     * @创建人: [choco]
     * @创建时间: [2020-07-30 13:41:32]
     * @修改人: []
     * @修改时间: []
     * @修改备注: []
     */
    @Override
    public User findUserByName(String username) {
        User dbUser = null;


        User user = new User();
        //设置名称
        user.setUserName(username);

        // 根据用户名查
        List<User> users = userMapper.selectUserList(user);
        if (ObjectUtil.isEmpty(users) || ObjectUtil.length(users) < 1) {
            user.setUserName(null);
            // 设置email
            user.setEmail(username);
            users = userMapper.selectUserList(user);
            // 根据email查
        }
        if (ObjectUtil.isEmpty(users) || ObjectUtil.length(users) < 1) {
            user.setEmail(null);
            user.setPhonenumber(username);
            // 根据手机查
            users = userMapper.selectUserList(user);
        }

        if (ObjectUtil.isEmpty(users) || ObjectUtil.length(users) < 1) {
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), username, "用户名不存在", "1"));
            throw new UnknownAccountException("用户名不存在");

        }
        dbUser = users.get(0);
        if (dbUser.getStatus().equals("1")) {
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), username, "用户已经锁用", "1"));
            throw new LockedAccountException("用户已经锁用");
        }
        if (dbUser.getDelFlag().equals("2")) {
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), username, "用户已被删除", "1"));
            throw new AuthenticationException("用户已被删除");
        }

        return dbUser;
    }

    @Override
    public void login(UsernamePasswordToken token) {
        try {
            // 获取主体
            Subject subject = SecurityUtils.getSubject();
            //登录，会到shiro中reaml认证方法中执行认证
            subject.login(token);
            // 登录成功，日志
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), token.getUsername(), "登录成功", "0"));
        }catch (IncorrectCredentialsException e){
            log.error(e.getMessage());
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), token.getUsername(), "用户或密码不正确", "1"));
        }catch (AuthenticationException e) {
            log.error(e.getMessage());
            AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(ServletUtils.getRequest(), token.getUsername(), "登录失败", "1"));
        }
    }

}
